<?php
/**
 * 微信通知接口设置
 *
 *
 *
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */

use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class applet_msgControl extends SystemControl{
    private $links = array(
        array('url'=>'act=applet_msg&op=index','text'=>'基本设置'),
        array('url'=>'act=applet_msg&op=msg_list','text'=>'模板列表')
    );
    public function __construct(){
        parent::__construct();
    }

    /**
     * 接口设置
     */
    public function indexOp(){
        Tpl::output('top_link',$this->sublink($this->links, 'index'));
        $model_setting = Model('setting');
        if (chksubmit()){
            $update_array = array();
            $update_array['weixin_applet_isuse']   = $_POST['weixin_applet_isuse'];
            $update_array['weixin_applet_appid']   = $_POST['weixin_applet_appid'];
            $update_array['weixin_applet_appsecret']  = $_POST['weixin_applet_appsecret'];
            $update_array['weixin_applet_token_array']  = '';//清空微信的数据缓存
            $result = $model_setting->updateSetting($update_array,true);
            if ($result){
                $this->log('小程序通知接口设置');
                showMessage(Language::get('nc_common_save_succ'));
            } else {
                showMessage(Language::get('nc_common_save_fail'));
            }
        }
        $list_setting = $model_setting->getListSetting();
        Tpl::output('list_setting',$list_setting);
        Tpl::showpage('applet_msg.edit');
    }

    /**
     * 接口模板列表
     */
    public function msg_listOp(){
        Tpl::output('top_link',$this->sublink($this->links, 'msg_list'));
        $logic_applet_notice = Logic('applet_notice');
        $access_token = $this->get_token();
        $list = $logic_applet_notice->getAllTemplate($access_token);

       /* $template_ids = array_column($list,'template_id');

        $switch = Model('member_msg_tpl')->getMemberMsgTplList(array('applet_msg_id'=>array('in',$template_ids)),array('mmt_applet_switch','applet_msg_id'));

        $template_ids_switchs = array_column($switch,'mmt_applet_switch','applet_msg_id');

        foreach($list as &$val){
            $val['status'] = !empty($template_ids_switchs[$val['template_id']]);
        }*/

        Tpl::output('list', $list);
        
        Tpl::showpage('applet_msg.list');
    }

    public function tmp_handle(){
        $template_id = $_GET['template_id'];
        $model_member_msg_tpl = Model('member_msg_tpl');
        $condition = array('applet_msg_id'=>$template_id);
        $find = $model_member_msg_tpl->getMemberMsgTplInfo($condition,array('mmt_applet_switch'));

        if(isset($find['mmt_applet_switch'])){
            if($find['mmt_applet_switch']){
                $update = array('mmt_applet_switch'=>0);
            }else{
                $update = array('mmt_applet_switch'=>1);
            }
            $model_member_msg_tpl->editMemberMsgTpl($condition,$update);
        }else{

        }


    }

    /**
     * 获取access_token
     */
    public function get_token(){
        $logic_applet_notice = Logic('applet_notice');
        $access_token = $logic_applet_notice->getAccessToken();
        if (empty($access_token)) $access_token = $logic_applet_notice->getAccessToken(1);
        return $access_token;
    }

}
